/// **************************************************************************************************
/// "Copyright (c) 2007 Sai Panyam, Inc.  All rights reserved."
/// **************************************************************************************************
/// Component Name   :	FootnoteModifier, Member of System72.NTextile 
/// Description      :	FootnoteModifier description
/// Author           :	Sai Panyam
/// Creation Date    :	November 09 2007
/// Modified Date    :              
/// Modified By      :
/// **************************************************************************************************
using System;
using System.Text.RegularExpressions;
using System72.NTextile.RegexLibrary;

namespace System72.NTextile
{
    internal class FootnoteModifier : IModifier
    {
        #region Private Variables

        private readonly Operation m_operation = new Operation();
        private AttributeCollection m_supportedAttributes;
        private PunctuationCollection m_supportedPunctuations;
        private bool m_success;
        private readonly int m_parseOrder=7;
        private readonly ModifierType m_type;
        
        #endregion

        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        public FootnoteModifier()
        {
            m_operation.Tag = "p";
            m_operation.Operator = "fn.";
            m_type = ModifierType.Footnote;
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// 
        /// </summary>
        public AttributeCollection SupportedAttributes
        {
            get
            {
                return m_supportedAttributes;
            }
            set
            {
                m_supportedAttributes = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public PunctuationCollection SupportedPunctuations
        {
            get { return m_supportedPunctuations; }
            set { m_supportedPunctuations = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool Success
        {
            get { return m_success; }
            set { m_success = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int ParseOrder
        {
            get { return m_parseOrder; }
        }
        /// <summary>
        /// 
        /// </summary>
        public ModifierType Type
        {
            get { return m_type; }
        }

        #endregion
        
        #region Public Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contentText"></param>
        /// <param name="excludedModifiers"></param>
        /// <returns></returns>
        public string GetXHTML(string contentText, ModifierCollection excludedModifiers)
        {
            if (m_operation != null)
            {
                if (!excludedModifiers.ContainsModifier(this))
                {
                    FootnoteRegex footnoteRegEx = new FootnoteRegex();
                    MatchCollection matchCollection = footnoteRegEx.GetMatches(contentText);
                    foreach (Match match in matchCollection)
                    {
                        //Since there is atleast one match then we can set Success to true
                        m_success = true;
                        string matchText = match.Value;
                        GroupCollection gc = match.Groups;
                        if (gc != null && gc.Count > 0)
                        {
                            string footNoteID = String.Empty;
                            CaptureCollection cc = gc[1].Captures;
                            if (cc != null && cc.Count > 0)
                            {
                                footNoteID = cc[0].Value;
                                //Remove the period at the end of the footnote id.
                                if (footNoteID.EndsWith("."))
                                {
                                    footNoteID = footNoteID.Remove(footNoteID.Length - 1, 1);
                                }
                            }
                            string footNoteRef = gc[2].Value;
                            string mainText = gc[3].Value;
                            string attributeValue = gc[4].Value;
                            if (footNoteID != String.Empty && footNoteRef != String.Empty)
                            {
                                matchText = "(#" + footNoteID + ")" + attributeValue + "<sup>" + footNoteRef + "</sup>" + mainText;
                            }
                        }
                    
                        string finalAttribute = String.Empty;
                        if (m_supportedAttributes!=null && m_supportedAttributes.Count>0)
                        {
                            foreach (Attribute attribute in m_supportedAttributes)
                            {
                                string matchValue;
                                string attributeValue = m_operation.GetAttributeText(matchText, attribute, out matchValue);
                                attribute.AttributeValue = attributeValue;
                                string attributeKeyValue = attribute.GetAttributeKeyValueText();
                                if (matchValue == String.Empty)
                                {
                                    attributeKeyValue = String.Empty;
                                }
                                else
                                {
                                    matchText = matchText.Replace(matchValue, "");
                                }
                                finalAttribute = finalAttribute + attributeKeyValue;
                            }
                        }
                        matchText = Util.GetElementMarkUp(matchText, m_operation.Tag, finalAttribute);
                        //TODO: Replace only the operator, Not any instance of it in the text
                        matchText = matchText.Replace(m_operation.Operator, "");

                        //Do the HTML only if any content is present between the operators
                        if (match.Value.Length > (m_operation.Operator.Length * 2))
                        {
                            contentText = contentText.Replace(match.Value, matchText);
                        }
                    }
                }
            }
            return contentText;
        }

        #endregion

    }
}
